from django.http.response import JsonResponse, HttpResponse
from django.urls import path
from lucommon.logger import lu_logger



def open_debug(request):
    try:
        from pydevd import settrace
        # todo 这里只能进入一次debug，结束后状态无法重置
        lu_logger.info("init to link remote debug server")
        settrace(
            host="127.0.0.1",
            port=5678,
            stderrToServer=True,
            stdoutToServer=False,
            suspend=True,
            trace_only_current_thread=True
        )
        lu_logger.info("pydevd set trace")
        code, message = 0, ""
    except Exception as e:
        code, message = -1, str(e)

    return JsonResponse({"code": code, "message": message})


def ready(request):
    # 服务健康检查
    return HttpResponse("ready")


urlpatterns = [
    path('open-debug', open_debug),
    path('ready', ready),
]
